//
//  BaseModel.m
//  
//
//  Created by 郭明皓 on 15/10/19.
//
//

#import "BaseModel.h"

@implementation BaseModel


+(NSMutableArray *)baseModelArry:(NSArray *)arr
{
    //先初始化容器arr
    NSMutableArray *modelArr = [NSMutableArray array];
    //遍历数组
    for (NSDictionary *dic in arr) {
        @autoreleasepool {
            
            //通过便利构造器来创建对象
            id model = [[self class]baseModelWithDic:dic];//获取调用类的类型
            [modelArr addObject:model];//放到数组里,引用计数+1
        }
    
    }
    return modelArr;
}

+ (instancetype)baseModelWithDic:(NSDictionary *)dic
{
    //通过多态来创建对象
    id model = [[[[self class]alloc]initWithDic:dic]autorelease];
    
    return model;
}

- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if ( self) {
        //进行kvc的赋值
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}

+(BOOL) isConnectionAvailable{
    
    BOOL isExistenceNetwork = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            isExistenceNetwork = NO;
            //NSLog(@"notReachable");
            break;
        case ReachableViaWiFi:
            isExistenceNetwork = YES;
            //NSLog(@"WIFI");
            break;
        case ReachableViaWWAN:
            isExistenceNetwork = YES;
            //NSLog(@"3G");
            break;
    }
    
    if (!isExistenceNetwork) {
     
        return NO;
    }
    
    return isExistenceNetwork;
}

@end
